﻿/********************************************************************************/
// <copyright file="NewsService.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
// <history>
//     <change who="Phuoc Le" date="21/12/2012 6:02:37 PM">Created</change>
// </history>
/********************************************************************************/

using System;
using System.Collections.Generic;
using System.Data.Linq;
using JLL.China.Model;
using JLL.China.Infrastructure.RepositoryFramework;
using JLL.China.Infrastructure.Repositories;
using JLL.China.Infrastructure.Repositories.News;

namespace JLL.China.Infrastructure.Services
{
    public static class NewsService
    {
        private static INewsRepository repository;

        static NewsService()
        {
            repository = RepositoryFactory.GetRepository<INewsRepository, Model.News, int>(new LINQ2SQLUnitOfWork<int>());
        }

        public static Model.News FindNews(int id)
        {
            return repository.FindBy(id);
        }

        /*public static void CreateNews(News news )
        {
            repository.Add(news);
        }*/
        public static IList<Model.News> GetByCategory(int id)
        {
            return repository.FindByNewsCategory(id);
        }
        public static void Save()
        {
            repository.UnitOfWork.Commit();

        }

        public static void ChangeStatus(string arrIds, bool status)
        {
            repository.ChangeStatus(arrIds,status);
        }

        public static void RemoveSelected(string arrIds)
        {
            repository.RemoveSelected(arrIds);
        }


        public static Model.News FindByID_Language(decimal? id, decimal languageId)
        {
            return repository.FindByID_Language(id,  languageId);
        }


        public static decimal CreateNews(Model.News news, Model.News newsLocal)
        {
            try
            {
                newsLocal.DateCreated = news.DateCreated;
                newsLocal.DateUpdated = news.DateUpdated;
                newsLocal.Status = news.Status;
                newsLocal.CreatedBy = news.CreatedBy;
                newsLocal.Thumbnail = news.Thumbnail;
                newsLocal.CreatedBy = news.CreatedBy;
                newsLocal.UpdatedBy = news.UpdatedBy;
                
                newsLocal.NewCategoryFID = news.NewCategoryFID;
                decimal id = repository.CreateNews(news);
                newsLocal.NewsId = id;
                return repository.CreateNews(newsLocal);
            }
            catch (Exception)
            {

                return -1;
            }

        }

      
        public static decimal UpdateNews(Model.News news,Model.News newsLocal)
        {
            try
            {
                newsLocal.DateCreated = news.DateCreated;
                newsLocal.DateUpdated = news.DateUpdated;
                newsLocal.Status = news.Status;
                newsLocal.CreatedBy = news.CreatedBy;
                
                newsLocal.Thumbnail = news.Thumbnail;
                newsLocal.CreatedBy = news.CreatedBy;
                newsLocal.UpdatedBy = news.UpdatedBy;
                newsLocal.NewsId = news.NewsId;
                newsLocal.NewCategoryFID = news.NewCategoryFID;
                decimal id = repository.UpdateNews(news);
                return repository.UpdateNews(newsLocal);
            }
            catch (Exception)
            {
                return -1;
            }

        }

        public static  decimal UpdateNews(Model.News news)
        {
            return repository.UpdateNews(news);
        }

        public static decimal DeleteNews(decimal? newsId)
        {
            return repository.DeleteNews(newsId);
        }

        public static Binary GetThumbnail(decimal newsId)
        {
            return repository.GetThumbnail(newsId);
        }
       
    }
}
